package cn.tedu.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.tedu.dao.EmpDao;
import cn.tedu.entity.Emp;
import cn.tedu.util.JsonResult;

@Service("empService")
public class EmpServiceImpl implements EmpService{

	@Resource
	private EmpDao empDao;
	public JsonResult<List<Emp>> empList() {
		JsonResult<List<Emp>> result = new JsonResult<List<Emp>>();
		List<Emp> list = empDao.findAll();
		
		if (list.size() == 0) {
			result.setStatus(1);
			result.setMsg("娌℃湁鏌ヨ鍒颁换浣曞憳宸ヤ俊鎭�");
			result.setData(list);
			return result;
		}
		
		result.setStatus(0);
		result.setData(list);
		return result;
	}
	public JsonResult<Object> addEmp(String name, String salary, String age) {
		// TODO Auto-generated method stub
		JsonResult<Object> result = new JsonResult<Object>();
		
		Emp emp = new Emp();
		emp.setName(name);
		emp.setSalary(Double.parseDouble(salary));
		emp.setAge(Integer.parseInt(age));
		empDao.save(emp);
	
		result.setStatus(0);
		result.setMsg("添加成功");
		return result;
	}
	@Override
	public JsonResult<Object> deleteEmp(int id) {
		JsonResult<Object> result = new JsonResult<Object>();
		int rwos = empDao.delete(id);
		
		if (rwos==0) {
			result.setStatus(1);
			result.setMsg("删除失败");
			return result;
		}else {
			result.setStatus(0);
			result.setMsg("删除成功");
			return result;
		}
	}
	@Override
	public JsonResult<Object> findById(int id) {
		// TODO Auto-generated method stub
		JsonResult<Object> result = new JsonResult<Object>();
		Emp emp = empDao.findById(id);
		if (emp==null) {
			result.setStatus(1);
			result.setMsg("无法查询到员工信息");
			return result;
		}
		result.setStatus(0);
		result.setMsg("查询成功");
		result.setData(emp);
		return result;
	}
	@Override
	public JsonResult<Object> updateEmp(int id,String name, double salary,int age) {
		// TODO Auto-generated method stub
		JsonResult<Object> result = new JsonResult<Object>();
		Emp emp = new Emp();
		emp.setId(id);
		emp.setName(name);
		emp.setSalary(salary);
		emp.setAge(age);
		int update = empDao.update(emp);
		
		if (update==1) {
			
			result.setStatus(0);
			result.setMsg("成功");
			return result;
		}else {
			result.setStatus(1);
			result.setMsg("失败");
			return result;
		}
	}

}
